<?php
include('../../includes/classes/core.php');

if($_REQUEST['act'] == 'get_list'){
    $count = 		$_REQUEST['count'];
    $hidden = 		$_REQUEST['hidden'];
    $start =        $_REQUEST['start'];
    $end =          $_REQUEST['end'];
    $mizn =          $_REQUEST['mizn'];

    $mizn_check = "";
    if($mizn == 1){
        $mizn_check = "AND ((task_scenar.status_1 = 1 AND task_scenar.status_2 = 1) OR task_scenar.status_3 = 1)";
    }else{
        $mizn_check = "";
    }
    
    $rResult = mysql_query("SELECT
	`task_detail`.`id` AS `id`,
	`out_call`.`calldate` AS `date`,
	persons.`name`,
	`phone`.`first_last_name` AS `first_last_name`,
    `phone`.`phone1`,
IF (
	(`task_scenar`.`status_1` = 1),
	_utf8 'კი',

IF (
	(`task_scenar`.`status_1` = 2),
	_utf8 'არა',
	_utf8 ''
)
) AS `status_p`,

IF (
	(`task_scenar`.`status_2` = 1),
	_utf8 'კი',

IF (
	(`task_scenar`.`status_2` = 2),
	_utf8 'არა',
	_utf8 ''
)
) AS `status_pp`,

IF (
	(`task_scenar`.`status_3` = 1),
	_utf8 'კი',
	_utf8 ''
) AS `status_ppp`,
  task_detail.call_content,
  SEC_TO_TIME(MAX(`out_call`.`duration`)) AS `duration1`,
  MAX(`out_call`.`duration`) AS `duration`,
  ROUND(((MAX(`out_call`.`duration`) * 0.005333333)),2) AS `sum`,
  IF(out_call.`server` = 1,0,ROUND(((MAX(`out_call`.`duration`) * 0.0012)),2)) AS `sum`
FROM `task_detail`
JOIN users ON task_detail.responsible_user_id = users.id
JOIN persons ON users.person_id = persons.id
JOIN `task_scenar` ON `task_detail`.`id` = `task_scenar`.`task_detail_id`
JOIN `phone` ON `task_detail`.`phone_base_id` = `phone`.`id`
JOIN `out_call` ON `phone`.`phone1` = `out_call`.`phone` AND out_call.`status` = 'ANSWERED' AND out_call.`duration` > 15
WHERE `task_detail`.`status` = 3 AND DATE(`task_scenar`.`date`) >= '$start' AND DATE(`task_scenar`.`date`) <= '$end' $mizn_check
GROUP BY out_call.phone
HAVING MAX(out_call.duration)
");
     
    $data = array(
        "aaData"	=> array()
    );
    
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i = 0 ; $i < $count ; $i++ )
        {
            /* General output */
            $row[] = $aRow[$i];
        }
        $data['aaData'][] = $row;
    }
    
    echo json_encode($data);
}
?>